<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>生成器</title>
</head>
<body>
    
</body>
<script>
    //生成器实际是一个特殊的函数，用于避免回调地狱
    function * gen(){
        yield 'E G M';
        console.log('执行第一句');
        yield '欧力给！';
        console.log('执行第二句');
        yield '这么神奇？！你能帮帮我?';
        console.log('执行第三句');
    }

    for (const iterator of gen()) {
        console.iterator;
    }

    let iterator = gen();
    console.log(iterator.next());
    console.log(iterator.next());
    console.log(iterator.next());
    console.log(iterator.next());

    console.log('____________________________分隔符____________________________');

    //生成器参数 实现异步编程
    function * gen2(args){
        console.log(args);
        let one = yield 'E G M';
        console.log(one);
        let two = yield '欧力给！';
        console.log(two);
        let three = yield '这么神奇？！你能帮帮我?';
        console.log(three);
    }
    let iterator2 = gen2('起始参数');
    console.log(iterator2.next());
    console.log(iterator2.next('111'));// next参数将作为第2个yield的返回结果
    console.log(iterator2.next('222'));// next参数将作为第3个yield的返回结果
    console.log(iterator2.next('333'));// next参数将作为第4个yield的返回结果

    console.log('____________________________分隔符____________________________');

    //实例1  1s后输出111，2s后输出222，3s后输出333
    // setTimeout(()=>{
    //     console.log('111');
    //         setTimeout(()=>{
    //             console.log('222');
    //                 setTimeout(()=>{
    //                     console.log('333');
    //                 },3000)
    //         },2000)
    // },1000);

    console.log('使用生成器实现');
    function one(){
        setTimeout(()=>{
            console.log('111');
            iterator3.next();
        },1000)
    }
    function two(){
        setTimeout(()=>{
            console.log('222');
            iterator3.next();
        },2000)
    }
    function three(){
        setTimeout(()=>{
            console.log('333');
            iterator3.next();
        },3000)
    }

    function * gen3(params) {
        yield one();
        yield two();
        yield three();
    }

    let iterator3 = gen3();
    iterator3.next();


    //实例2 //模拟数据获取  用户数据，订单数据，商品数据
    function getUsers() {
        setTimeout(()=>{
            let data = '用户数据';
            itorator4.next(data);
        },1000);
    }

    function getOrders() {
        setTimeout(()=>{
            let data = '订单数据';
            itorator4.next(data);
        },1000);
    }

    function getGoods() {
        setTimeout(()=>{
            let data = '商品数据';
            itorator4.next(data);
        },1000);
    }

    function * gen4(){
        let users = yield getUsers();
        console.log(users);
        let orders = yield getOrders();
        console.log(orders);
        let goods = yield getGoods();
        console.log(goods);
    }

    let itorator4 = gen4();
    itorator4.next();



</script>
</html>